package com.gyhy.dumplings.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyhy.dumplings.common.dto.WareDto;
import com.gyhy.dumplings.product.dao.ProductDao;
import com.gyhy.dumplings.product.entity.ProductEntity;
import com.gyhy.dumplings.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: 光影灰翼
 * @date: 2022-09-26 8:37
 * @version: 1.0
 * @email: 978330688@qq.com
 */
@Service
public class ProductServiceImpl  extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {

    @Autowired
    private ProductDao productDao;

    public List<ProductEntity> listProducts(){
        return productDao.selectList(null);
    }

    public List<WareDto> listWareDto(List<WareDto> wareDtoList) {
        // sql查找对应的数据库
        List<Integer> productIds=wareDtoList.stream().map(WareDto::getProductId).collect(Collectors.toList());
        QueryWrapper<ProductEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("product_id",productIds);
        List<ProductEntity> productEntityList= productDao.selectList(queryWrapper);
        //给wareDtoList添加wareId信息
        for (WareDto wareDto:wareDtoList) {
            for (ProductEntity productEntity:productEntityList) {
                if(wareDto.getProductId().equals(productEntity.getProductId())){
                    wareDto.setWareId(productEntity.getWareId());
                    wareDto.setCost(productEntity.getCost());
                    break;
                }
            }
        }
        System.out.println(wareDtoList);

        return wareDtoList;
    }
}
